package com.example.order.Service.imply;

import com.example.order.Service.OrderService;
import com.example.order.dao.BrandDAO;
import com.example.order.dao.OdersDAO;
import com.example.order.dao.WareDAO;
import com.example.order.entity.BrandEntity;
import com.example.order.entity.OdersEntity;
import com.example.order.entity.WareEntity;
import com.example.order.openFeign.UserOpenFeign;
import com.example.vo.OrderVIO;
import com.example.vo.UserVIO;
import com.example.vo.WareVio;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransactionContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service("OrderService")
public class OrderServiceImply1 implements OrderService {
    /**
     * 创建订单
     * 首先查询仓库是否有这么多的库存，
     * 然后通知查看用户账户是否有这么多的金额
     * 如果都有，扣除库存，扣除金额
     *
     * @param orderVIO
     * @return
     */
    @Autowired
    WareDAO wareDAO;
    @Autowired
    UserOpenFeign userOpenFeign;
    @Autowired
    BrandDAO brandDAO;
    @Autowired
    OdersDAO odersDAO;
    @GlobalTransactional
    @Transactional
    @Override
    public boolean createOrder(OrderVIO orderVIO) throws TransactionException {
        BrandEntity brandEntity = brandDAO.getBrandDetails(orderVIO.getBrandId(),orderVIO.getWaraId());
        WareEntity wareEntity = getWareEntity(orderVIO.getWaraId());
        UserVIO userVIO = userOpenFeign.getusercountdetails(orderVIO.getUserId());
        int r=0;
        if (null != brandEntity && null != wareEntity) {
            long moneys = orderVIO.getBuyCount() * brandEntity.getBrandPrice();

            //扣除库存
            WareVio wareVio = new WareVio();
            BeanUtils.copyProperties(wareEntity, wareVio);
            wareVio.setBuyCount(orderVIO.getBuyCount());
            int ch = wareDAO.deductWare(wareVio);
            if(ch==0 ){
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
                return  false;
            }
            //扣除金额
            userVIO.setCutMoney(moneys);
            boolean check = userOpenFeign.cutmoneyByUserId(userVIO);

            if(!check){
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
                return  false;
            }
            //创建订单
            OdersEntity odersEntity = new OdersEntity();
            odersEntity.setUserId(orderVIO.getUserId());
            odersEntity.setWareId(orderVIO.getWaraId());
            odersEntity.setWareCount(orderVIO.getBuyCount());
            odersEntity.setDate(new Date());
            r = odersDAO.addOrder(odersEntity);
            System.out.println(odersEntity);
        } else {

            return false;
        }
        return r == 1;
    }

    public WareEntity getWareEntity(Long wareId) {
        return wareDAO.getWare(wareId);
    }

}
